package mz.co.research.server.mvc.services;

import mz.co.framework.context.IUserContext;
import mz.co.framework.context.UserContextHolder;
import mz.co.framework.error.AbstractException;
import mz.co.framework.error.SystemException;
import mz.co.research.server.mvc.model.dao.jpa.ResearchJpaDao;
import mz.co.research.shared.model.Research;
import mz.co.research.shared.services.IResearchService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ResearchService extends AbstractService<ResearchService> implements
		IResearchService {

	@Autowired
	private ResearchJpaDao researchJpaDao;

	@Transactional
	public void createResearch(IUserContext context, Research research)
			throws AbstractException {
		researchJpaDao.persist(research);
	}

	@Transactional
	public void activeResearch(IUserContext context, Research research,
			boolean actve) throws AbstractException {

		research.setActive(actve);
		researchJpaDao.merge(research);
	}

	@Transactional
	public Research updateResearch(IUserContext context, Research research)
			throws AbstractException {
		return researchJpaDao.merge(research);
	}

	public Research findResearchById(IUserContext context, Long id)
			throws SystemException {
		return researchJpaDao.findById(id);
	}

	@Transactional
	public void gwtCreateResearch(Research research) throws AbstractException {
		// FIXME : Enquanto nao tiver processo de Login.
		UserContextHolder.setContext(UserContextHolder.getStubContext());
		researchJpaDao.persist(research);
	}

}
